/**
 * @file router
 * @author lishaohai
 */

import Vue from 'vue';
import Router from 'vue-router';
// import Home from '@/views/home/index';
import Nofind from '@/views/nofind/index';
import Login from '@/views/login/index';

// import Layout from '@/layout/index';

import {
    LAYOUT,
    LOGIN,
    HOME,
    BILL,
    ABOUT,
    BILL_DETAIL,
    REVENUE,
    EXPENDITURE,
    ROLE,
    AUTH,
    PLATFORM,
    PERMISSION_ADMIN,
    PERMISSION_MODULE_LAYOUT,
    PERMISSION_MODULE_LAYOUT_ADD,
    PERMISSION_MODULE_LAYOUT_DETAIL,
    PERMISSION_MODULE_LAYOUT_EDIT,
    PERMISSION_MODULE_CHILD,
    PERMISSION_MODULE_PARENT,
    PERMISSION_FUNCTION,
    PERMISSION_FUNCTION_ADD,
    PERMISSION_FUNCTION_DETAIL,
    PERMISSION_FUNCTION_EDIT,
    PERMISSION_INTERFACE,
    PERMISSION_INTERFACE_RECOVERY,
    ACCOUNT_WECHAT,
    ACCOUNT_ALIPAY,
    ACCOUNT_MERCHANTS,
    ACCOUNT_ALIPAY_TRANSCACTIONS,
    ACCOUNT_ALIPAY_TRANSCACTIONS_HISTORY,
    PROFILE,
    BILL_LIST,
    BILL_LIST_DETAIL,
    BILL_EDIT,
    FINANCIAL_ITEM_IN,
    FINANCIAL_ITEM_OUT,
    FINANCIAL_TRANSFER,
    REPORT_ACCOUNT,
    REPORT_VENDOR,
    REPORT_CUSTOMER,
    REPORT_ITEM_PROJECT,
    REPORT_PROFIT,
    SYSTEM_ACCOUNT,
    SYSTEM_CAR,
    SYSTEM_CUSTOMER,
    SYSTEM_VENDOR,
    SYSTEM_PERSON,
    SYSTEM_PERSON_RECOVERY,
    SYSTEM_ACCOUNT_RECOVERY,
    SYSTEM_CAR_RECOVERY,
    SYSTEM_CUSTOMER_RECOVERY,
    SYSTEM_VENDOR_RECOVERY,
    SYSTEM_DEPOT_RECOVERY,
    SYSTEM_ITEM_OUT_IN,
    SYSTEM_DEPOT,
    ORDER_INDEX,
    ORDER_REPORT,
    XSYX_HOME,
    XSYX_GRAPH,
    XSYX_ORDER,
    LOG_LIST
} from '@/router/route-name';

/**
 * 重写路由的push方法
 */
const routerPush = Router.prototype.push;
Router.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error);
};

Vue.use(Router);

// 静态路由
export const constantRoutes = [
    {
        path: '*',
        name: '/404',
        meta: {title: 'Not Found'},
        component: Nofind
    }
];

// 需要远程获取
export const asyncRoutesMap = {
    [LAYOUT]: () => import(/* webpackChunkName: "layout" */ '@/layout/index'),
    [HOME]: () => import(/* webpackChunkName: "home" */ '@/views/home/index'),
    // [BILL]: () => import(/* webpackChunkName: "bill" */ '@/views/bill/index'),
    [BILL_EDIT]: () => import(/* webpackChunkName: "bill-edit" */ '@/views/bill/index'),
    [BILL_DETAIL]: () => import(/* webpackChunkName: "bill" */ '@/views/bill/detail'),
    [PLATFORM]: () => import(/* webpackChunkName: "platform" */ '@/views/bill/platform/index'),
    [REVENUE]: () => import(/* webpackChunkName: "revenue" */ '@/views/revenue/index'),
    [EXPENDITURE]: () => import(/* webpackChunkName: "expenditure" */ '@/views/expenditure/index'),
    [ROLE]: () => import(/* webpackChunkName: "role" */ '@/views/system/role/index'),
    [AUTH]: () => import(/* webpackChunkName: "user" */ '@/views/system/user/index'),

    // [PERMISSION_ADMIN]: () => import(/* webpackChunkName: "permission" */ '@/views/system/permission/index'),
    [PERMISSION_MODULE_LAYOUT]: () =>
        import(/* webpackChunkName: "moduleLayout" */ '@/views/system/moduleLayout/index'),
    [PERMISSION_MODULE_LAYOUT_ADD]: () =>
        import(/* webpackChunkName: "moduleLayout-add" */ '@/views/system/moduleLayout/detail'),
    [PERMISSION_MODULE_LAYOUT_DETAIL]: () =>
        import(/* webpackChunkName: "moduleLayout-detail" */ '@/views/system/moduleLayout/detail'),
    [PERMISSION_MODULE_LAYOUT_EDIT]: () =>
        import(/* webpackChunkName: "moduleLayout-edit" */ '@/views/system/moduleLayout/detail'),
    // eslint-disable-next-line max-len
    // [PERMISSION_MODULE_CHILD]: () => import(/* webpackChunkName: "moduleChild" */ '@/views/system/moduleChild/index'),
    // [PERMISSION_MODULE_PARENT]: () =>
    // import(/* webpackChunkName: "moduleParent" */ '@/views/system/moduleParent/index'),
    [PERMISSION_FUNCTION]: () => import(/* webpackChunkName: "functionMap" */ '@/views/system/functionMap/index'),
    [PERMISSION_FUNCTION_ADD]: () => import(/* webpackChunkName: "functionMap" */ '@/views/system/functionMap/detail'),
    [PERMISSION_FUNCTION_DETAIL]: () =>
        import(/* webpackChunkName: "functionMap" */ '@/views/system/functionMap/detail'),
    [PERMISSION_FUNCTION_EDIT]: () => import(/* webpackChunkName: "functionMap" */ '@/views/system/functionMap/detail'),
    [PERMISSION_INTERFACE]: () => import(/* webpackChunkName: "interface" */ '@/views/system/interface/index'),
    [PERMISSION_INTERFACE_RECOVERY]: () =>
        import(/* webpackChunkName: "interface-recovery" */ '@/views/system/interface/recovery'),

    [ABOUT]: () => import(/* webpackChunkName: "about" */ '@/views/about/index'),
    [ACCOUNT_WECHAT]: () => import(/* webpackChunkName: "account-wechat" */ '@/views/account/wechat/index'),
    [ACCOUNT_ALIPAY]: () => import(/* webpackChunkName: "account-alipay" */ '@/views/account/alipay/index'),
    [ACCOUNT_ALIPAY_TRANSCACTIONS]: () =>
        import(
            // eslint-disable-next-line max-len
            /* webpackChunkName: "account-alipay-transcations" */ '@/views/account/alipay/transcations'
        ),
    [ACCOUNT_ALIPAY_TRANSCACTIONS_HISTORY]: () =>
        import(
            /* webpackChunkName: "account-alipay-transcations-history" */ '@/views/account/alipay/transcations-history'
        ),
    [ACCOUNT_MERCHANTS]: () => import(/* webpackChunkName: "account-merchants" */ '@/views/account/merchants/index'),
    [PROFILE]: () => import(/* webpackChunkName: "profile" */ '@/views/profile/index'),
    [BILL_LIST]: () => import(/* webpackChunkName: "bill-list" */ '@/views/bill-list/index'),
    [BILL_LIST_DETAIL]: () => import(/* webpackChunkName: "bill-detail" */ '@/views/bill/detail'),
    [FINANCIAL_ITEM_IN]: () => import(/* webpackChunkName: "financial-item-in" */ '@/views/financial/item-in'),
    [FINANCIAL_ITEM_OUT]: () => import(/* webpackChunkName: "financial-item-out" */ '@/views/financial/item-out'),
    [FINANCIAL_TRANSFER]: () => import(/* webpackChunkName: "financial-transfer" */ '@/views/financial/transfer'),
    [REPORT_ACCOUNT]: () => import(/* webpackChunkName: "report-account" */ '@/views/report/account'),
    [REPORT_VENDOR]: () => import(/* webpackChunkName: "report-vendor" */ '@/views/report/vendor'), // 供应商
    [REPORT_CUSTOMER]: () => import(/* webpackChunkName: "system-account" */ '@/views/report/customer'), // 客户
    [REPORT_ITEM_PROJECT]: () =>
        import(/* webpackChunkName: "system-item-project" */ '@/views/report/item-project/index'), // 资金流水
    [REPORT_PROFIT]: () => import(/* webpackChunkName: "system-profit" */ '@/views/report/profit'), // 资金流水
    [SYSTEM_ACCOUNT]: () => import(/* webpackChwebpackChunkName: "system-account" */ '@/views/system/account'), // 客户
    [SYSTEM_ACCOUNT_RECOVERY]: () =>
        import(/* webpackChwebpackChunkName: "system-account" */ '@/views/system/account/recovery'), // 客户
    [SYSTEM_CAR]: () => import(/* webpackChunkName: "system-car" */ '@/views/system/car'),
    [SYSTEM_CAR_RECOVERY]: () => import(/* webpackChunkName: "system-car-recovery" */ '@/views/system/car/recovery'),
    [SYSTEM_CUSTOMER]: () => import(/* webpackChunkName: "system-customer" */ '@/views/system/customer'),
    [SYSTEM_CUSTOMER_RECOVERY]: () =>
        import(/* webpackChunkName: "system-customer-recovery" */ '@/views/system/customer/recovery'),
    [SYSTEM_VENDOR]: () => import(/* webpackChunkName: "system-vendor" */ '@/views/system/vendor'),
    [SYSTEM_VENDOR_RECOVERY]: () =>
        import(/* webpackChunkName: "system-vendor-recovery" */ '@/views/system/vendor/recovery'),

    [SYSTEM_PERSON]: () => import(/* webpackChunkName: "system-person" */ '@/views/system/person'),
    [SYSTEM_PERSON_RECOVERY]: () =>
        import(/* webpackChunkName: "system-person-recovery" */ '@/views/system/person/recovery'),
    [SYSTEM_DEPOT]: () => import(/* webpackChunkName: "system-depot" */ '@/views/system/depot'),
    [SYSTEM_DEPOT_RECOVERY]: () =>
        import(/* webpackChunkName: "system-depot-recovery" */ '@/views/system/depot/recovery'),
    [SYSTEM_ITEM_OUT_IN]: () => import(/* webpackChunkName: "system-item-out-in" */ '@/views/system/item-out-in'),
    [ORDER_INDEX]: () => import(/* webpackChunkName: "order-index" */ '@/views/order/index'),
    [ORDER_REPORT]: () => import(/* webpackChunkName: "order-report" */ '@/views/order/report'),
    [XSYX_HOME]: () => import(/* webpackChunkName: "bill-detail" */ '@/views/xsyx/index'),
    [XSYX_GRAPH]: () => import(/* webpackChunkName: "xsyx-users-graph" */ '@/views/xsyx/graph'),
    [XSYX_ORDER]: () => import(/* webpackChunkName: "xsyx-users" */ '@/views/xsyx/order'),
    [LOG_LIST]: () => import(/* webpackChunkName: "log-list" */ '@/views/loglist/index')
};

// 创建路由实例
const createRouter = () =>
    new Router({
        mode: 'hash',
        // mode: 'history',
        base: process.env.publicPath,
        routes: [
            {
                path: '/',
                redirect: '/login'
            },
            {
                path: '/login',
                name: LOGIN,
                meta: {title: '登录'},
                component: Login
            }
        ],

        scrollBehavior: () => ({y: 0})
    });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter();
    router.matcher = newRouter.matcher; // reset router
}

export default router;
